import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:${USER}
 * Date:${YEAR}-${MONTH}-${DAY}
 * ${TIME}
 */
public class Main {
    int[] dx={0,0,-1,1};
    int[] dy={1,-1,0,0};
    int m,n;
    boolean[][] visted;
    public void solve(char[][] board) {
        m=board.length;
        n=board[0].length;
        visted=new boolean[m][n];
        for (int i = 0; i < n; i++) {
            if(visted[0][i]==false&&board[0][i]=='O'){
                bfs(board,0,i);
            }
            if(visted[m-1][i]==false&&board[m-1][i]=='O'){
                bfs(board,m-1,i);
            }
        }
        for (int i = 0; i < m; i++) {
            if(visted[i][0]==false&&board[i][0]=='O'){
                bfs(board,i,0);
            }
            if(visted[i][n-1]==false&&board[i][n-1]=='O'){
                bfs(board,i,n-1);
            }
        }
        for(int i=0;i<m;i++){
            for (int j = 0; j < n; j++) {
                if(visted[i][j]==false&&board[i][j]=='O'){
                    bfs1(board,i,j);
                }
            }
        }
    }

    public void bfs(char[][] grid,int i,int j){
        Queue<int[]> queue=new LinkedList<>();
        visted[i][j]=true;
        queue.add(new int[]{i,j});

        while (!queue.isEmpty()){
            int[] arr=queue.poll();
            int a=arr[0];int b=arr[1];
            for (int k = 0; k < 4; k++) {
                int x=a+dx[k];int y=b+dy[k];
                if(x>=0&&x<m&&y>=0&&y<n&&visted[x][y]==false&&grid[x][y]=='O'){
                    visted[x][y]=true;
                    queue.add(new int[]{x,y});
                }
            }
        }

    }
    public void bfs1(char[][] grid,int i,int j){
        Queue<int[]> queue=new LinkedList<>();
        visted[i][j]=true;
        queue.add(new int[]{i,j});
        grid[i][j]='X';
        while (!queue.isEmpty()){
            int[] arr=queue.poll();
            int a=arr[0];int b=arr[1];
            for (int k = 0; k < 4; k++) {
                int x=a+dx[k];int y=b+dy[k];
                if(x>=0&&x<m&&y>=0&&y<n&&visted[x][y]==false&&grid[x][y]=='O'){
                    visted[x][y]=true;
                    grid[x][y]='X';
                    queue.add(new int[]{x,y});
                }
            }
        }

    }
}